<!DOCTYPE html>
<meta charset="utf-8" />
<title>Geolocation Test: non-fully active document</title>
<link rel="help" href="https://github.com/w3c/geolocation-api/pull/97" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="support.js"></script>
<body></body>
<script>
  promise_setup(async () => {
    await test_driver.set_permission({ name: "geolocation" }, "granted");
  });

  promise_test(async (t) => {
    // Create the iframe, wait for it to load...
    const iframe = document.createElement("iframe");
    await new Promise((resolve) => {
      iframe.src = "resources/iframe.html";
      iframe.allow = "geolocation";
      iframe.addEventListener("load", resolve, { once: true });
      document.body.appendChild(iframe);
    });

    // Steal geolocation.
    const geo = iframe.contentWindow.navigator.geolocation;

    // No longer fully active.
    iframe.remove();

    // Try to watch a position while not fully active...
    const watchError = await new Promise((resolve, reject) => {
      const watchId = geo.watchPosition(
        reject, // We don't want a position
        resolve // We want an error!
      );
      // Always return 0.
      assert_equals(
        watchId,
        0,
        "watchId is 0 when document is not fully-active"
      );
      // And again, to make sure it's not changing
      const watchId2 = geo.watchPosition(
        () => {},
        () => {}
      );
      assert_equals(
        watchId2,
        0,
        "watchId remains 0 when document is not fully-active"
      );
    });

    assert_equals(
      watchError.code,
      GeolocationPositionError.POSITION_UNAVAILABLE,
      "watchPosition() returns an error on non-fully-active document"
    );

    // Now try to get current position while not fully active...
    const positionError = await new Promise((resolve, reject) => {
      geo.getCurrentPosition(
        reject, // We don't want a position
        resolve // We want an error!
      );
    });
    assert_equals(
      positionError.code,
      GeolocationPositionError.POSITION_UNAVAILABLE,
      "getCurrentPosition() calls the errorCallback with POSITION_UNAVAILABLE"
    );

    // Re-attach, and go back to fully active.
    document.body.appendChild(iframe);
    iframe.contentWindow.opener = window;
    await new Promise((resolve) =>
      iframe.addEventListener("load", resolve, { once: true })
    );

    // And we are back to fully active.
    let watchId;
    let position = await new Promise((resolve, reject) => {
      watchId = iframe.contentWindow.navigator.geolocation.watchPosition(
        resolve,
        reject
      );
    });
    assert_true(Number.isInteger(watchId), "Expected some number for watchId");
    assert_true(Boolean(position), "Expected a position");

    // Finally, let's get the position from the reattached document.
    position = await new Promise((resolve, reject) => {
      iframe.contentWindow.navigator.geolocation.getCurrentPosition(
        resolve,
        reject
      );
    });
    assert_true(Boolean(position), "Expected a position");
    iframe.contentWindow.navigator.geolocation.clearWatch(watchId);
  }, "non-fully active document behavior");
</script>
